package javaStu.producerAndConsumer;

/**
 * 生产消费模式
 * 用两个线程，一个输出字母，一个输出数字，交替输出1A2B3C4D5E...26Z
 *
 * @author: leiwenkang
 * @date: 2020/7/13
 * @time: 20:22
 */
public class ProducerAndConsumer_WaitNotify2 {
    public static void main(String[] args) {
        final Object o = new Object();

        char[] ch1 = "1234567".toCharArray();
        char[] ch2 = "ABCDEFG".toCharArray();

        new Thread(() -> {
            synchronized (o) {
                for (char c : ch1) {
                    System.out.print(c);
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        }, "t1").start();


        new Thread(() -> {
            synchronized (o) {
                for (char c : ch2) {
                    System.out.print(c);
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        }, "t2").start();

    }

}
